package com.xexun.gpstracker.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Window;
import com.xexun.gpstracker.channel.R;
import com.xexun.gpstracker.common.Constants;
import com.xexun.gpstracker.util.Utils;

public class DialogActivity extends Activity {
    private AlertDialog alertDialog;
    private MediaPlayer mediaPlayer;
    private Context context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        context = this;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
       // VibrateDialog.getInstance().showDialog(this, false);

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(context.getString(R.string.vibrate_dialog_bludisconnect)).setPositiveButton(context.getString(R.string.vibrate_dialog_ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                bleDisconnected(context);
                finish();
            }
        });
        alertDialog = builder.create();
        alertDialog.setCancelable(false);
        //alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        //alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.show();

        if (Utils.getPreferences(context).getBoolean(Constants.MSG_VOICE, true)) {
            mediaPlayer = MediaPlayer.create(context, R.raw.alarm);
            mediaPlayer.setVolume(1.0f, 1.0f);
            mediaPlayer.setLooping(true);
            mediaPlayer.start();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void bleDisconnected(Context context) {
        context.sendBroadcast(new Intent(Constants.VIBRATOR_CLOSE));
        if (null != alertDialog) {
            alertDialog.dismiss();
            alertDialog.cancel();
            alertDialog = null;
        }
        if (null != mediaPlayer) {
            mediaPlayer.stop();
        }
    }
}